﻿Imports System
Imports System.IO
Imports System.Text



Public Class Functions
    Inherits Connect

    'FeatureChainの関数--------------------------------------------------------------------------------------
    '直線のFeatureChainを作成する関数 'MEMO 130719 使用しない関数
    Public Function CreateFeatureChain(ByVal PointNum As Integer, ByVal Point() As Esprit.Point, ByVal Name As String, ByRef Key As String) As Boolean
        If PointNum < 2 Then Return False

        Dim FeatureChain As Esprit.FeatureChain

        FeatureChain = Document.FeatureChains.Add(Point(0))
        For i As Integer = 0 To PointNum - 2
            FeatureChain.Add(Document.GetSegment(Point(i), Point(i + 1)))
        Next
        FeatureChain.Add(Document.GetSegment(Point(PointNum - 1), Point(0)))

        With FeatureChain
            .Grouped = True
            .Name = Name
            Key = .Key
        End With

        Document.Refresh()

        Return True
    End Function

    '直線のFeatureChain を作成する関数 'MEMO 130730 使用しない関数
    Public Function CreateFeatureChainSegment(ByVal SegmentNum As Integer, ByVal StartPoint As Esprit.Point, ByVal Segments() As Esprit.Segment, ByVal Name As String, ByRef Key As String) As Boolean
        Dim FeatureChain As Esprit.FeatureChain
        FeatureChain = Document.FeatureChains.Add(StartPoint)

        For i As Integer = 0 To SegmentNum - 1
            FeatureChain.Add(Segments(i))
        Next
        With FeatureChain
            .Grouped = True
            .Name = Name + " (" + .Key + ")"
            Key = .Key
        End With

        Document.Refresh()

        Return True
    End Function

    'Segmentを作成する関数
    Public Function CreateSegment(ByVal StartingPoint As Esprit.Point, ByVal EndPoint As Esprit.Point) As Esprit.Segment
        Dim Segment As Esprit.Segment

        Segment = Document.Segments.Add(StartingPoint, EndPoint)

        Return Segment
    End Function

    'Arcを作成する関数
    Public Function CreateArc(ByVal CenterPoint As Esprit.Point, ByVal Radious As Double, ByVal StartAngle As Double, ByVal EndAngle As Double) As Esprit.Arc
        Dim Arc As Esprit.Arc

        Arc = Document.Arcs.Add(CenterPoint, Radious, StartAngle, EndAngle)

        Return Arc
    End Function

    'FeaturePtoPを作成する関数
    Public Function CreateFeaturePtoP(ByVal CenterPoint As Esprit.Point, _
                                      ByVal Radious As Double, _
                                      ByVal Depth As Double, _
                                      ByVal Name As String, _
                                      ByVal IsThrough As Boolean, _
                                      ByRef Key As String) As Esprit.FeaturePtop

        Dim FeaturePtoP As Esprit.FeaturePtop
        FeaturePtoP = Document.FeaturePtops.Add(CenterPoint)

        With FeaturePtoP
            .Name = Name + " (" + .Key + ")"
            .Diameter = Radious * 2
            .Depth = Depth
            Key = .Key
            .Through = IsThrough
            .LineWeight = EspritConstants.espLineWeight.espLineWeightThin
        End With

        Document.Refresh()

        Return FeaturePtoP
    End Function

    '直線のFeatureChain を作成する関数
    Public Function CreateFeatureChainSegment(ByVal SegmentNum As Integer, _
                                              ByVal StartPoint As Esprit.Point, _
                                              ByVal Segments() As Esprit.Segment, _
                                              ByVal Name As String, ByRef Key As String, _
                                              ByVal Depth As Double, _
                                              ByVal IsInside As Boolean, _
                                              ByVal IsUpSide As Boolean) As Esprit.FeatureChain
        Dim FeatureChain As Esprit.FeatureChain
        Dim X(SegmentNum) As Double
        Dim Y(SegmentNum) As Double
        Dim CentreX, CentreY As Double
        Dim Angle(1) As Double
        Dim IsCW As Boolean

        FeatureChain = Document.FeatureChains.Add(StartPoint)
        '各Segmentの開始点を取得して，FeatureChainの中心点を計算
        CentreX = 0
        CentreY = 0
        For i As Integer = 0 To SegmentNum - 1
            X(i) = Segments(i).XStart
            Y(i) = Segments(i).YStart
            CentreX += X(i)
            CentreY += Y(i)
        Next
        CentreX /= SegmentNum
        CentreY /= SegmentNum

        '中心と１つめ，２つ目の角度を計算してCWかCCWかを判断
        For i As Integer = 0 To 1
            Angle(i) = Math.Atan2((Y(i) - CentreY), (X(i) - CentreX))
            If Angle(i) < 0 Then Angle(i) += 2 * Math.PI
        Next
        If Angle(0) < Angle(1) Then
            IsCW = False
        Else : IsCW = True
        End If

        If IsUpSide Then Depth = -Depth

        'Segmentを利用してFeatureChainを作成
        For i As Integer = 0 To SegmentNum - 1
            FeatureChain.Add(Segments(i))

        Next
        'FeatureChainの各Propertyを設定
        With FeatureChain
            .Name = Name + " (" + .Key + ")"
            Key = .Key
            .Depth = Depth
            'IsInsideとIsCwを利用してFeatureChainのどちら側を切削するか決定
            If (IsCW And IsInside) Or (Not IsCW And Not IsInside) Then .CutSide = EspritConstants.espOffsetSide.espOffsetRight
            If (Not IsCW And IsInside) Or (IsCW And Not IsInside) Then .CutSide = EspritConstants.espOffsetSide.espOffsetLeft
            .LineWeight = EspritConstants.espLineWeight.espLineWeightThin
        End With

        Document.Refresh()

        Return FeatureChain
    End Function

    '円弧のFeatureChainを作成する関数
    Public Function CreateFeatureChainArc(ByVal ArcNum As Integer, _
                                          ByVal StartPoint As Esprit.Point, _
                                          ByVal Arcs() As Esprit.Arc, _
                                          ByVal Name As String, _
                                          ByRef Key As String, _
                                          ByVal Depth As Double, _
                                          ByVal IsInside As Boolean, _
                                          ByVal IsUpSide As Boolean) As Esprit.FeatureChain

        Dim FeatureChain As Esprit.FeatureChain
        FeatureChain = Document.FeatureChains.Add(StartPoint)

        If IsUpSide Then Depth = -Depth

        For i As Integer = 0 To ArcNum - 1
            FeatureChain.Add(Arcs(i))
        Next
        With FeatureChain
            .Grouped = True
            .Name = Name + " (" + .Key + ")"
            Key = .Key
            If IsInside Then : .CutSide = EspritConstants.espOffsetSide.espOffsetRight
            Else : .CutSide = EspritConstants.espOffsetSide.espOffsetLeft
            End If
            .Depth = Depth
            .LineWeight = EspritConstants.espLineWeight.espLineWeightThin
        End With

        Document.Refresh()

        Return FeatureChain

    End Function


    '加工フィーチャ関連の関数------------------------------------------------------------------------------------------------------------------
    'システムで認識するFeatureの面の数を返す関数
    Public Function NumberOfFacesInFeature(ByVal Index As Integer) As Integer
        Select Case Index
            Case 0
                Return 1
            Case 1 To 4, 6, 9, 11, 12, 14
                Return 6
            Case 5
                Return 7
            Case 7, 8, 10, 13
                Return 3
            Case Else
                '認識出来ないフィーチャの場合は手動で入力
                Dim FaceNum As Integer
                Dim Message As String = "Input the Number of Faces in Feature (Code:" + CStr(Index) + ")"
                FaceNum = CInt(InputBox(Message))
                Return FaceNum
        End Select
    End Function

    'システムで認識するFeatureの名前を返す関数
    Public Function NameOfFeature(ByVal Index As Integer) As String
        Select Case Index
            Case 0
                Return "Face"
            Case 1
                Return "Closed_Pocket"
            Case 2
                Return "Open_Pocket"
            Case 3
                Return "Square_Hole"
            Case 4
                Return "Closed_Slot"
            Case 5
                Return "Open_Slot"
            Case 6
                Return "Step"
            Case 7
                Return "Through_Hole"
            Case 8
                Return "Blind_Hole"
            Case 9
                Return "Square_Boss"
            Case 10
                Return "Circle_Boss"
            Case 11
                Return "Edge_Boss"
            Case 14
                Return "Corner_Boss"
            Case 12
                Return "Square_Base"
            Case 13
                Return "Circle_Base"
            Case Else
                Return "Unknown_Feature"

        End Select
    End Function

    'システムで認識するFeatureのInSide OR OutSideを返す関数
    Public Function IsInSideOrNot(ByVal Index As Integer) As Boolean
        Select Case Index
            Case 1, 2, 3, 4, 5, 6, 7, 8
                Return True
            Case 9, 11, 14, 10
                Return False
        End Select
    End Function

    'システムで認識するFeatureのUpSide OR DownSideを返す関数
    Public Function IsUpSideOrNot(ByVal Index As Integer) As Boolean
        Select Case Index
            Case 1, 2, 4, 5, 6, 8, 9
                Return True
            Case 3, 7, 11, 14, 10
                Return False
        End Select
    End Function



    'Technologyの関数---------------------------------------------------------------------------------------------
    '工具情報を設定する関数
    Public Function SetTool(ByVal ToolID As String, _
                            ByVal ToolType As Integer, _
                            ByVal ToolDiameter As Double, _
                            ByVal CuttingLength As Double, _
                            ByVal FlutesNumber As Integer, _
                            ByVal ToolLength As Double, _
                            ByVal ToolMaterial As Integer, _
                            ByVal ToolCoating As Integer, _
                            ByVal ShankDiamter As Double) As EspritTechnology.Technology

        Dim Technology As EspritTechnology.Technology
        Dim TechnologyUtility As EspritTechnology.TechnologyUtility

        TechnologyUtility = CType(Document.TechnologyUtility, EspritTechnology.TechnologyUtility)

        Dim ToolStyle As Integer
        Select Case ToolType
            Case 1
                Technology = CType(TechnologyUtility.CreateTechnology(EspritConstants.espTechnologyType.espToolMillFaceMill, EspritConstants.espUnitType.espMetric), EspritTechnology.Technology)
            Case 2
                Technology = CType(TechnologyUtility.CreateTechnology(EspritConstants.espTechnologyType.espToolMillEndMill, EspritConstants.espUnitType.espMetric), EspritTechnology.Technology)
            Case 3
                Technology = CType(TechnologyUtility.CreateTechnology(EspritConstants.espTechnologyType.espToolMillBallMill, EspritConstants.espUnitType.espMetric), EspritTechnology.Technology)
            Case 4
                Technology = CType(TechnologyUtility.CreateTechnology(EspritConstants.espTechnologyType.espToolMillDrill, EspritConstants.espUnitType.espMetric), EspritTechnology.Technology)
        End Select

        Dim ToolMaterialAndCoating As String = "N/A"
        Select Case ToolMaterial
            Case 1
                ToolMaterialAndCoating = "Carbide"
            Case 2
                ToolMaterialAndCoating = "CBN"
            Case 3
                ToolMaterialAndCoating = "Ceramic"
            Case 4
                ToolMaterialAndCoating = "High-Speed Steel"
        End Select
        If ToolCoating = 1 Then
            ToolMaterialAndCoating += ",Coated"
        Else
            ToolMaterialAndCoating += ",UnCoated"
        End If

        If Not Technology Is Nothing Then
            With Technology
                .Item("ToolID").Value = ToolID
                .Item("ToolStyle").Value = ToolStyle
                .Item("ToolDiameter").Value = ToolDiameter
                .Item("CuttingLength").Value = CuttingLength
                Select Case ToolType
                    Case 1
                        .Item("NumberOfInserts").Value = FlutesNumber
                    Case 2, 3, 4
                        .Item("NumberOfFlutes").Value = FlutesNumber
                End Select

                .Item("ToolLength").Value = ToolLength
                .Item("ToolMaterial").Value = ToolMaterial + ToolCoating
                .Item("ShankDiameter").Value = ShankDiamter
            End With
        End If

        Return Technology
    End Function

    '各TechnologyType毎のMachiningModeを返す関数
    Public Function setMachiningStrategy(ByVal MachiningMode As Integer, ByVal MachiningStrategy As Integer) As Integer
        Select Case MachiningMode
            Case 1
                'フェースミル加工
                Select Case MachiningStrategy
                    Case 1
                        '一方向-ダウンカット
                        Return 0
                    Case 2
                        '一方向-アップカット
                        Return 1
                    Case 3
                        '双方向
                        Return 2
                    Case Else
                        MsgBox("ERROR")
                        Return 0
                End Select
            Case 2
                '輪郭加工
                Select Case MachiningStrategy
                    Case 1
                        '深さ方向から
                        Return 1
                    Case 2
                        '幅方向から
                        Return 2
                End Select
            Case 3
                'ポケット加工
                Select Case MachiningStrategy
                    Case 1
                        '一方向
                        Return 0
                    Case 2
                        'ジグザグ
                        Return 1
                    Case 3
                        '同心円-In
                        Return 2
                    Case 4
                        '同心円-Out
                        Return 3
                    Case Else
                        MsgBox("ERROR")
                        Return 0
                End Select
            Case 4

                'スパイラル加工
                'TODOスパイラス加工のMachiningStrategyを調査
                Return 0
            Case Else
                MsgBox("本システムでは対応していない加工法です")
                Return 0
        End Select
    End Function

    '各TechnologyType毎のMachiningModeの設定値をEspritで使える形で返す
    Public Function getMachinigStrategy(ByVal MachiningMode As Integer, ByVal MachiningStrategy As Integer) As Integer
        Select Case MachiningMode
            Case 1
                'フェースミル加工
                Select Case MachiningStrategy
                    Case 0
                        '一方向-ダウンカット
                        Return 1
                    Case 1
                        '一方向-アップカット
                        Return 2
                    Case 2
                        '双方向
                        Return 3
                    Case Else
                        MsgBox("ERROR")
                        Return -1
                End Select
            Case 2
                '輪郭加工
                Return -1
            Case 3
                'ポケット加工
                Select Case MachiningStrategy
                    Case 1
                        '一方向
                        Return 0
                    Case 2
                        'ジグザグ
                        Return 1
                    Case 3
                        '同心円-In
                        Return 2
                    Case 4
                        '同心円-Out
                        Return 3
                    Case Else
                        MsgBox("ERROR")
                        Return -1
                End Select
            Case 4
                'ドリル加工
                'TODOドリル加工のMachiningStrategyを調査
                Return -1
            Case 5
                'スパイラル加工
                'TODOスパイラス加工のMachiningStrategyを調査
                Return -1
            Case Else
                MsgBox("本システムでは対応していない加工法です")
                Return -1
        End Select
    End Function

    'フェースミル加工を新しく作成
    Sub CreateMillFaceTechnology(ByVal Key As String, _
                                 ByVal OperationName As String, _
                                 ByVal ToolID As String, _
                                 ByVal SpindleSpeed As Long, _
                                 ByVal XYFeedrate As Double, _
                                 ByVal ZFeedrate As Double, _
                                 ByVal MachiningMode As Integer, _
                                 ByVal MachiningStrategy As Long, _
                                 ByVal StepOver As Double, _
                                 ByVal TotalDepth As Double, _
                                 ByVal IncrementalDepth As Double, _
                                 ByVal StartingDepth As Double, _
                                 ByVal StockAllowanceFloor As Double)

        Dim TechnologyUtility As EspritTechnology.TechnologyUtility
        Dim Technology As EspritTechnology.Technology
        Dim Operation As Esprit.Operation
        Dim FeatureChain As Esprit.FeatureChain

        ' Technology = SetTool("EM 04.0", 1, 4, 16, 4, 12)
        ' Document.Tools.Add(Technology)

        'Technologyを作成
        TechnologyUtility = CType(Document.TechnologyUtility, EspritTechnology.TechnologyUtility)
        Technology = CType(TechnologyUtility.CreateTechnology(EspritConstants.espTechnologyType.espTechMillFace, EspritConstants.espUnitType.espMetric), EspritTechnology.Technology)
        If Not Technology Is Nothing Then
            With Technology
                '設定するパラメータの設定
                If MachiningMode = 2 Then
                    '双方向送りの場合
                    .Item("Strategy").Value = 0
                ElseIf MachiningStrategy = 0 Then
                    '一方向送りの場合
                    'ダウンカットに設定
                    .Item("Strategy").Value = 0
                Else
                    'アップカットに設定
                    .Item("Strategy").Value = 1
                End If
                .Item("OperationName").Value = OperationName
                .Item("StartingDepth").Value = StartingDepth
                .Item("IncrementalDepth").Value = IncrementalDepth
                .Item("XYFeedratePM").Value = XYFeedrate
                .Item("TotalDepth").Value = TotalDepth
                .Item("ZFeedratePM").Value = ZFeedrate
                .Item("SpindleSpeedRPM").Value = SpindleSpeed
                .Item("StepOver").Value = StepOver
                .Item("ToolID").Value = ToolID
                .Item("StockAllowanceFloors").Value = StockAllowanceFloor
            End With
        End If
        FeatureChain = Document.FeatureChains.Item(Key)
        Operation = Document.Operations.Add(Technology, FeatureChain)

    End Sub

    'ポケットミル加工を新しく作成
    Sub CreateMillPocketTechnology(ByVal Key As String, _
                                   ByVal OperationName As String, _
                                   ByVal ToolID As String, _
                                   ByVal SpindleSpeed As Long, _
                                   ByVal XYFeedrate As Double, _
                                   ByVal ZFeedrate As Double, _
                                   ByVal MachiningMode As Integer, _
                                   ByVal MachiningStrategy As Long, _
                                   ByVal StepOver As Double, _
                                   ByVal TotalDepth As Double, _
                                   ByVal IncrementalDepth As Double, _
                                   ByVal StartingDepth As Double, _
                                   ByVal StockAllowanceWall As Double, _
                                   ByVal StockAllowanceFloor As Double)

        Dim TechnologyUtility As EspritTechnology.TechnologyUtility
        Dim Technology As EspritTechnology.Technology
        Dim Operation As Esprit.Operation
        Dim FeatureChain As Esprit.FeatureChain
        Dim IslandNum As Integer
        Dim IslandFeatureChain() As Esprit.FeatureChain       'MEMO 130812 現時点では10以上のIslandFeatureを想定していない

        '     Technology = SetTool("EM 04.0", 1, 4, 16, 4, 12)
        '     Document.Tools.Add(Technology)


        'Technologyを作成
        TechnologyUtility = CType(Document.TechnologyUtility, EspritTechnology.TechnologyUtility)
        Technology = CType(TechnologyUtility.CreateTechnology(EspritConstants.espTechnologyType.espTechMillPocket2, EspritConstants.espUnitType.espMetric), EspritTechnology.Technology)
        If Not Technology Is Nothing Then
            With Technology
                '設定するパラメータの設定
                .Item("CuttingStrategy").Value = MachiningStrategy
                .Item("OperationName").Value = OperationName
                .Item("StartingDepth").Value = StartingDepth
                .Item("IncrementalDepth").Value = IncrementalDepth
                .Item("XYFeedratePM").Value = XYFeedrate
                .Item("TotalDepth").Value = TotalDepth
                .Item("ZFeedratePM").Value = ZFeedrate
                .Item("SpindleSpeedRPM").Value = SpindleSpeed
                .Item("StepOver").Value = StepOver
                .Item("ToolID").Value = ToolID
                .Item("ToolMotionPattern").Value = MachiningMode
                .Item("StockAllowanceWalls").Value = StockAllowanceWall
                .Item("StockAllowanceFloors").Value = StockAllowanceFloor
                .Item("IslandFeatures").Value = ""
            End With
        End If
        FeatureChain = Document.FeatureChains.Item(Key)

        'Dim i As Integer = 0
        'Do While Err.Number = 0
        '    i += 1
        '    ReDim Preserve IslandFeatureChain(i)
        '    FeatureChainKey = InputBox("Select FeatureChainKey of IslandFeature #" + CStr(i))
        '    IslandFeatureChain(i) = Document.FeatureChains.Item(FeatureChainKey)
        'Loop
        'ReDim Preserve IslandFeatureChain(UBound(IslandFeatureChain) - 1)
        'Err.Clear()



        'IslandNum = CInt(InputBox("Input the Number of Island Features"))
        'If Not IslandNum = 0 Then
        '    For i As Integer = 0 To IslandNum - 1
        '        FeatureChainKey = InputBox("Select FeatureChainKey of IslandFeature #" + CStr(i))
        '        IslandFeatureChain(i) = Document.FeatureChains.Item(FeatureChainKey)
        '    Next
        'End If
        'ReDim Preserve IslandFeatureChain(IslandNum - 1)



        'On Error Resume Next
        '
        'Dim i As Integer = 0
        'Do While Err.Number = 0
        '    i = i + 1
        '    ReDim Preserve IslandFeatureChain(i)
        '    IslandFeatureChain(i) = CType(Document.GetAnyElement("Select Island - press escape to process", EspritConstants.espGraphicObjectType.espFeatureChain), Esprit.FeatureChain)
        'Loop
        'ReDim Preserve IslandFeatureChain(UBound(IslandFeatureChain) - 1)
        'Err.Clear()

        Operation = Document.Operations.Add(Technology, FeatureChain, IslandFeatureChain)

        TechnologyUtility = Nothing
        Technology = Nothing
        Operation = Nothing
        FeatureChain = Nothing

    End Sub

    '輪郭ミル加工を新しく作成
    Sub CreateMillContourTechnology(ByVal Key As String, _
                                    ByVal OperationName As String, _
                                    ByVal ToolID As String, _
                                    ByVal SpindleSpeed As Long, _
                                    ByVal XYFeedrate As Double, _
                                    ByVal ZFeedrate As Double, _
                                    ByVal MachiningMode As Integer, _
                                    ByVal MachiningStrategy As Long, _
                                    ByVal StepOver As Double, _
                                    ByVal TotalDepth As Double, _
                                    ByVal IncrementalDepth As Double, _
                                    ByVal StartingDepth As Double, _
                                    ByVal PassesNum As Integer, _
                                    ByVal StockAllowanceWall As Double,
                                    ByVal StockAllowanceFloor As Double)

        Dim TechnologyUtility As EspritTechnology.TechnologyUtility
        Dim Technology As EspritTechnology.Technology
        Dim Operation As Esprit.Operation
        Dim FeatureChain As Esprit.FeatureChain
        Dim CutSide As Integer

        '     Technology = SetTool("EM 04.0", 1, 4, 16, 4, 12)
        '     Document.Tools.Add(Technology)

        FeatureChain = Document.FeatureChains.Item(Key)
        CutSide = FeatureChain.CutSide

        'Technologyを作成
        TechnologyUtility = CType(Document.TechnologyUtility, EspritTechnology.TechnologyUtility)
        Technology = CType(TechnologyUtility.CreateTechnology(EspritConstants.espTechnologyType.espTechMillContour1, EspritConstants.espUnitType.espMetric), EspritTechnology.Technology)
        If Not Technology Is Nothing Then
            With Technology
                '設定するパラメータの設定
                .Item("ProcessOrder").Value = MachiningMode
                .Item("CuttingStrategy").Value = MachiningStrategy
                .Item("OperationName").Value = OperationName
                .Item("StartingDepth").Value = StartingDepth
                .Item("IncrementalDepth").Value = IncrementalDepth
                .Item("XYFeedratePM").Value = XYFeedrate
                .Item("TotalDepth").Value = TotalDepth
                .Item("ZFeedratePM").Value = ZFeedrate
                .Item("SpindleSpeedRPM").Value = SpindleSpeed
                .Item("StepOver").Value = StepOver
                .Item("ToolID").Value = ToolID
                .Item("RoughPasses").Value = PassesNum
                .Item("OffsetSideComputer").Value = CutSide
                .Item("StockAllowance").Value = StockAllowanceWall
                .Item("StockAllowanceFloors").Value = StockAllowanceFloor
            End With
        End If
        Operation = Document.Operations.Add(Technology, FeatureChain)

        TechnologyUtility = Nothing
        Technology = Nothing
        Operation = Nothing
        FeatureChain = Nothing

    End Sub

    'ドリル加工を新しく作成
    Sub CreateDrillTechnology(ByVal Key As String, _
                              ByVal OperationName As String, _
                              ByVal ToolID As String, _
                              ByVal SpindleSpeed As Long, _
                              ByVal ZFeedrate As Double, _
                              ByVal TotalDepth As Double, _
                              ByVal StartingDepth As Double)

        Dim TechnologyUtility As EspritTechnology.TechnologyUtility
        Dim Technology As EspritTechnology.Technology
        Dim Operation As Esprit.Operation

        Dim FeaturePtoP As Esprit.FeaturePtop = Document.FeaturePtops.Item(Key)

        'Technologyを作成
        TechnologyUtility = CType(Document.TechnologyUtility, EspritTechnology.TechnologyUtility)
        Technology = CType(TechnologyUtility.CreateTechnology(EspritConstants.espTechnologyType.espTechMillDrill, EspritConstants.espUnitType.espMetric), EspritTechnology.Technology)
        If Not Technology Is Nothing Then
            With Technology
                '設定するパラメータの設定
                .Item("OperationName").Value = OperationName
                .Item("StartingDepth").Value = StartingDepth
                .Item("ZFeedratePM").Value = ZFeedrate
                .Item("TotalDepth").Value = TotalDepth
                .Item("SpindleSpeedRPM").Value = SpindleSpeed
                .Item("ToolID").Value = ToolID
            End With
        End If
        'Operation = Document.Operations.Add(Technology)
        Operation = Document.Operations.Add(Technology, FeaturePtoP)

        TechnologyUtility = Nothing
        Technology = Nothing
        Operation = Nothing
        FeaturePtoP = Nothing



    End Sub

    'スパイラルミル加工を新しく作成
    Sub CreateMillSpiralTechnology(ByVal FeatureChainKey As String, ByVal OperationName As String, ByVal ToolID As String, ByVal SpindleSpeed As Long, ByVal XYFeedrate As Double, ByVal ZFeedrate As Double, ByVal MachiningMode As Integer, ByVal MachiningStrategy As Long, ByVal StepOver As Double, ByVal TotalDepth As Double, ByVal IncrementalDepth As Double, ByVal StartingDepth As Double)
        Dim TechnologyUtility As EspritTechnology.TechnologyUtility
        Dim Technology As EspritTechnology.Technology
        Dim Operation As Esprit.Operation
        Dim FeatureChain As Esprit.FeatureChain

        '       Technology = SetTool("EM 04.0", 1, 4, 16, 4, 12)
        '       Document.Tools.Add(Technology)


        'Technologyを作成
        TechnologyUtility = CType(Document.TechnologyUtility, EspritTechnology.TechnologyUtility)
        Technology = CType(TechnologyUtility.CreateTechnology(EspritConstants.espTechnologyType.espTechMillSpiral, EspritConstants.espUnitType.espMetric), EspritTechnology.Technology)
        If Not Technology Is Nothing Then
            With Technology
                '設定するパラメータの設定
                Select Case MachiningStrategy
                    Case 0
                        'ダウンカットの場合（パスを時計回りに設定）
                        .Item("Direction").Value = -1
                    Case 1
                        'アップカットの場合（パスを反時計周りに設定）
                        .Item("Direction").Value = 1
                End Select
                .Item("Direction").Value = MachiningStrategy
                .Item("OperationName").Value = OperationName
                .Item("StartingDepth").Value = StartingDepth
                .Item("IncrementalDepth").Value = IncrementalDepth
                .Item("XYFeedratePM").Value = XYFeedrate
                .Item("TotalDepth").Value = TotalDepth
                .Item("ZFeedratePM").Value = ZFeedrate
                .Item("SpindleSpeedRPM").Value = SpindleSpeed
                '.Item("StepOver").Value = StepOver
                .Item("ToolID").Value = ToolID
                .Item("Orientation").Value = MachiningMode
            End With
        End If
        FeatureChain = Document.FeatureChains.Item(FeatureChainKey)
        Operation = Document.Operations.Add(Technology, FeatureChain)

    End Sub

    '----------------------------------------------------------------------------------------------------------------

    'Code(in SW)とKey(in Esprit)を変換する関数----------------------------------------------------------------------
    'Code→Keyの変換関数
    Public Function Code2Key(ByVal Code As Integer) As Integer
        Dim Key As Integer
        Dim FeatureNum As Integer
        Dim retval(10) As String
        Dim Read As String

        '読み込み用のReader
        Dim path As String = "CodeKeyConverter.txt"
        Dim Reader As StreamReader = New StreamReader(path)

        Read = Reader.ReadLine
        retval = Read.Split(CChar(" "))

        FeatureNum = CInt(retval(0))

        For FeatureCounter As Integer = 0 To FeatureNum
            Read = Reader.ReadLine
            retval = Read.Split(CChar(" "))

            If Code = CInt(retval(0)) Then
                Key = CInt(retval(1))
            End If

        Next

        Return Key

    End Function

    'Key→Codeの変換関数
    Public Function Key2Code(ByVal Key As Integer) As Integer
        Dim Code As Integer
        Dim FeatureNum As Integer
        Dim retval(10) As String
        Dim Read As String

        '読み込み用のReader
        Dim path As String = "CodeKeyConverter.txt"
        Dim Reader As StreamReader = New StreamReader(path)

        Read = Reader.ReadLine
        retval = Read.Split(CChar(" "))

        FeatureNum = CInt(retval(0))

        For FeatureCounter As Integer = 0 To FeatureNum - 1
            Read = Reader.ReadLine
            retval = Read.Split(CChar(" "))

            If Key = CInt(retval(1)) Then
                Code = CInt(retval(0))
                Exit For
            End If

        Next

        Return Code

    End Function


    '被削材の類似度判定に使用する関数-------------------------------------------------------------------------------------
    '被削材の引張強さを返す関数
    Public Function MaterialStrength(ByVal Index As Integer) As Double
        Dim Strength As Double

        Select Case Index
            Case 1  'S45C
                Strength = 690
            Case 2  'SUS410
                Strength = 540
            Case 3  'SUS430
                Strength = 450
            Case 4  'SUS304
                Strength = 520
            Case 5  'A1085P
                Strength = 55
            Case 6  'A2017P
                Strength = 355
            Case 7  'A2024P
                Strength = 430
            Case 8  'チタン
                Strength = 320
        End Select

        Return Strength
    End Function

    '素材形状を作成する関数

    'TODO arcとsegmentの複合featurechain作成のprogram

    '----------------------------------------以下不要な関数-----------------------------------------------------------
    Public Function GetFeatureChainPoints(ByVal FeatureChain As Esprit.FeatureChain, ByRef ret() As Double) As Boolean
        Dim Point As Esprit.Point
        Dim length As Double
        Dim msg As String

        For i As Integer = 0 To 2
            Point = FeatureChain.Extremity(CType(i, EspritConstants.espExtremityType))
            msg = CStr(i) + ":" + CStr(Point.X) + "," + CStr(Point.Y) + "," + CStr(Point.Z)

        Next

        Return True

    End Function


    '円弧のFeatureChainを作成する関数
    'Public Function CreateFeatureChainArc(ByVal ArcNum As Integer, ByVal StartPoint As Esprit.Point, ByVal Arcs() As Esprit.Arc, ByVal Name As String, ByVal Key As String) As Boolean
    '    Dim FeatureChain As Esprit.FeatureChain
    '    FeatureChain = Document.FeatureChains.Add(StartPoint)
    '
    '    For i As Integer = 0 To ArcNum - 1
    '        FeatureChain.Add(Arcs(i))
    '    Next
    '    With FeatureChain
    '        .Grouped = True
    '        .Name = Name + " (" + .Key + ")"
    '        Key = .Key
    '    End With
    '
    '    Document.Refresh()
    '
    'End Function

    'Arcを作成する関数



End Class
